package com.huang.qqslidemenu.test;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * 拖拽布局
 * 
 * Created by huangRZ on 2016/9/14.
 */
public class DragLayout extends ViewGroup {

    private View redView;
    private View blueView;

    public DragLayout(Context context) {
        super(context);
    }

    public DragLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /** 指定测量规则 */
        int measureSpec =
                MeasureSpec.makeMeasureSpec(redView.getLayoutParams().width, MeasureSpec.EXACTLY);

        /** 对子View 进行测量(因为已知子View的宽高一样，所以宽高测量规则一样即可) */
        redView.measure(measureSpec, measureSpec);
        blueView.measure(measureSpec, measureSpec);//两个控件的宽高是一样的

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     *  成功加载完所有子View后，调用  一般用于获取子View操作 
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();//父类该方法为空
        //这里假设只要两个子View
        redView = getChildAt(0);
        blueView = getChildAt(1);
    }

    /**
     *  重写layout方法，按照自己的规则摆放子View  在onMeasure方法调用后执行
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left = getPaddingLeft();
        int top = getPaddingTop();
        redView.layout(left, top, redView.getMeasuredWidth(), redView.getMeasuredHeight());
        blueView.layout(left, top+redView.getMeasuredHeight(),blueView.getMeasuredWidth(),
                redView.getMeasuredHeight()+blueView.getMeasuredHeight());
    }
}
